/**
 * Project: eraclient
 * 
 * File Created at 2011-2-24
 * $Id: Login.java 127 2011-03-15 12:14:22Z ymkyve501@163.com $
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package login;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Login extends JFrame {

    JTextField name_txt, email_txt, verify_txt;
    JPasswordField passwd, passwd_sec;
    JRadioButton man, woman;
    JComboBox year, month, day;
    JLabel jl, picture;
    BufferedImage bi;
    String passcode;
    JCheckBox[] interest = new JCheckBox[12];
    JCheckBox selectAll, notSelect;
    JTextArea area;
    JButton confirm, concel, fileopen;

    public Login() {
        Container container = this.getContentPane();
        container.setLayout(null);
        JLabel mainLabel = new JLabel("<html><font color=red size=5 >欢迎注册");
        mainLabel.setBounds(200, 10, 100, 30);
        container.add(mainLabel);
        JLabel name = new JLabel("<html><font color=black size=4>用户名:");
        name.setBounds(10, 50, 50, 20);
        container.add(name);
        name_txt = new JTextField();
        name_txt.setBounds(80, 50, 200, 20);
        container.add(name_txt);
        JLabel pass = new JLabel("<html><font color=black size=4> 密码:");
        pass.setBounds(10, 80, 50, 20);
        container.add(pass);
        passwd = new JPasswordField();
        passwd.setBounds(80, 80, 200, 20);
        container.add(passwd);
        JLabel pass_sec = new JLabel("<html><font color=black size=4>再次输入");
        pass_sec.setBounds(10, 110, 60, 20);
        container.add(pass_sec);
        passwd_sec = new JPasswordField();
        passwd_sec.setBounds(80, 110, 200, 20);
        container.add(passwd_sec);
        JLabel sex = new JLabel("<html><font color=black size=4>性别:");
        sex.setBounds(10, 140, 50, 20);
        container.add(sex);

        man = new JRadioButton("男", true);
        woman = new JRadioButton("女");
        ButtonGroup bGroup = new ButtonGroup();
        bGroup.add(man);
        bGroup.add(woman);
        man.setBounds(80, 140, 40, 20);
        woman.setBounds(130, 140, 40, 20);
        container.add(man);
        container.add(woman);

        JLabel borthLabel = new JLabel("<html><font color=black size=4>出生年月:");
        borthLabel.setBounds(10, 175, 70, 20);
        container.add(borthLabel);
        JPanel datePanel = new JPanel();
        datePanel.setBounds(80, 170, 300, 30);
        datePanel.setLayout(new FlowLayout());
        year = new JComboBox();
        year.addItem("请选择..");
        initYear();
        year.addItemListener(new ItemEve());
        datePanel.add(year);
        datePanel.add(new JLabel("年"));
        month = new JComboBox();
        month.addItem("请选择..");
        month.addItemListener(new ItemEve());
        datePanel.add(month);
        datePanel.add(new JLabel("月"));
        day = new JComboBox();
        day.addItem("请选择..");
        datePanel.add(day);
        datePanel.add(new JLabel("日"));
        container.add(datePanel);

        JLabel verifying = new JLabel("<html><font color=black size=4>确认码");
        verifying.setBounds(10, 220, 50, 20);
        container.add(verifying);
        verify_txt = new JTextField();
        verify_txt.setBounds(80, 220, 200, 20);
        container.add(verify_txt);
        jl = new JLabel();
        jl.setBounds(300, 210, 60, 30);
        jl.setToolTipText("看不清,换一张");
        jl.addMouseListener(new Eve());
        bi = new BufferedImage(60, 30, BufferedImage.TYPE_INT_RGB);
        passcode = draw(bi);
        jl.setIcon(new ImageIcon(bi));
        container.add(jl);

        JSeparator jsp = new JSeparator();
        jsp.setBounds(10, 250, 400, 5);
        container.add(jsp);

        JLabel interestLabel = new JLabel(
                "<html><font color=black size=4>兴趣爱好:");
        interestLabel.setBounds(10, 260, 70, 20);
        container.add(interestLabel);

        JPanel interestPanel = new JPanel();
        interestPanel.setBounds(30, 280, 350, 80);
        String[] instereString = { "上网", "玩网游", "看电视", "读书", "打篮球", "踢足球",
                "唱歌", "跳舞", "跑步", "看报纸", "聊天", "旅游" };
        interestPanel.setLayout(new GridLayout(4, 4));
        for (int i = 0; i < interest.length; i++) {
            interest[i] = new JCheckBox(instereString[i]);
            interestPanel.add(interest[i]);
        }
        container.add(interestPanel);
        selectAll = new JCheckBox("全选");
        selectAll.addActionListener(new ActionEve());
        notSelect = new JCheckBox("反选");
        notSelect.addActionListener(new ActionEve());
        interestPanel.add(selectAll);
        interestPanel.add(notSelect);

        JLabel areaLabel = new JLabel("<html><font color=black size=4>其它");
        areaLabel.setBounds(10, 365, 60, 20);
        container.add(areaLabel);
        area = new JTextArea();
        area.setBounds(30, 390, 400, 100);
        container.add(area);

        confirm = new JButton("提交");
        confirm.setBounds(80, 520, 60, 30);
        container.add(confirm);
        confirm.addActionListener(new ActionEve());
        concel = new JButton("重置");
        concel.setBounds(250, 520, 60, 30);
        container.add(concel);
        concel.addActionListener(new ActionEve());

        picture = new JLabel();
        picture.setBounds(300, 20, 180, 150);
        container.add(picture);
        fileopen = new JButton("浏览");
        fileopen.addActionListener(new ActionEve());
        fileopen.setBounds(400, 180, 60, 20);
        container.add(fileopen);

        this.setSize(500, 600);
        this.setLocation(getLocation());
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public Point getLocation() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        return new Point((int) (dimension.getWidth() - 500) / 2,
                (int) (dimension.getHeight() - 600) / 2);
    }

    public void initYear() {
        for (int i = 0; i < 40; i++) {
            year.addItem(i + 1970 + "");
        }
    }

    public void initMonth() {
        for (int i = 1; i < 13; i++) {
            month.addItem(i + "");
        }
    }

    public void initDay(int ye, int mo) {
        int d = 31;
        if (mo != 2) {
            if ((mo % 2 == 0 && mo < 7) || (mo > 7 && mo % 2 != 0)) {
                d = 30;
            }
        } else {
            boolean b = leapYear(ye);
            if (b) {
                d = 29;
            } else {
                d = 28;
            }
        }
        for (int i = 1; i <= d; i++) {
            day.addItem(i + "");
        }
    }

    public boolean leapYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            return true;
        } else {
            return false;
        }
    }

    public String draw(BufferedImage bi) {
        Random rd = new Random();
        int rc, gc, bc, rc1, gc1, bc1, line1, line2, line3, line4;
        rc = rd.nextInt(150);
        gc = rd.nextInt(150);
        bc = rd.nextInt(150);
        Graphics g = bi.getGraphics();
        g.setColor(new Color(rc, gc, bc));
        g.fillRect(0, 0, 60, 30);
        rc1 = 100 + rd.nextInt(150);
        gc1 = 100 + rd.nextInt(150);
        bc1 = 100 + rd.nextInt(150);
        g.setColor(new Color(rc1, gc1, bc1));
        char[] ch = new char[4];
        ch[0] = (char) randomChar(rd);
        ch[1] = (char) randomChar(rd);
        ch[2] = (char) randomChar(rd);
        ch[3] = (char) randomChar(rd);
        String ma = new String(ch);

        int j = rd.nextInt(10);
        g.setFont(new Font("Rockwell", Font.BOLD, 20));
        g.drawString(ma, 5, 20 + j);
        rc = rd.nextInt(255);
        gc = rd.nextInt(255);
        bc = rd.nextInt(255);
        g.setColor(new Color(rc, gc, bc));
        line1 = 0;
        line2 = 60;
        line3 = rd.nextInt(30);
        line4 = rd.nextInt(30);
        g.drawLine(line1, line3, line2, line4);
        line3 = rd.nextInt(30);
        line4 = rd.nextInt(30);
        g.drawLine(line1, line3, line2, line4);
        line3 = rd.nextInt(30);
        line4 = rd.nextInt(30);
        g.drawLine(line1, line3, line2, line4);
        line3 = rd.nextInt(30);
        line4 = rd.nextInt(30);
        g.drawLine(line1, line3, line2, line4);
        return ma;

    }

    private int randomChar(Random rd) {
        int chma = rd.nextInt(62);
        if (chma < 10) {
            chma += 48;
        } else if (chma < 36) {
            chma += 55;
        } else {
            chma += 61;
        }
        return chma;
    }

    public static void main(String[] args) {

        new Login();

    }

    class ItemEve implements ItemListener {

        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                if (e.getSource() == year) {
                    if (year.getSelectedIndex() == 0) {
                        month.removeAllItems();
                        month.addItem("请选择..");
                        day.removeAllItems();
                        day.addItem("请选择..");

                    } else {
                        month.removeAllItems();
                        month.addItem("请选择..");
                        day.removeAllItems();
                        day.addItem("请选择..");
                        initMonth();
                    }
                }
                if (e.getSource() == month) {
                    if (month.getSelectedIndex() == 0) {
                        day.removeAllItems();
                        day.addItem("请选择..");
                    } else {
                        int y = Integer.parseInt(year.getSelectedItem()
                                .toString());
                        int m = Integer.parseInt(month.getSelectedItem()
                                .toString());
                        initDay(y, m);
                    }
                }
            }
        }

    }

    class Eve extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            passcode = draw(bi);
            jl.setIcon(new ImageIcon(bi));
        }

        public void mouseEntered(MouseEvent e) {
            Cursor cs = new Cursor(Cursor.HAND_CURSOR);
            jl.setCursor(cs);
        }

    }

    class ActionEve implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == selectAll) {
                boolean b = false;
                if (selectAll.isSelected()) {
                    b = true;
                }
                for (int i = 0; i < interest.length; i++) {
                    interest[i].setSelected(b);
                }
            }
            if (e.getSource() == notSelect) {
                for (int i = 0; i < interest.length; i++) {
                    if (interest[i].isSelected()) {
                        interest[i].setSelected(false);
                    } else {
                        interest[i].setSelected(true);
                    }
                }
            }
            if (e.getSource() == concel) {
                name_txt.setText(null);
                passwd.setText(null);
                passwd_sec.setText(null);
                man.setSelected(true);
                year.removeAllItems();
                year.addItem("请选择");
                initYear();
                verify_txt.setText(null);
                for (int i = 0; i < interest.length; i++) {
                    interest[i].setSelected(false);
                }
                selectAll.setSelected(false);
                notSelect.setSelected(false);
                area.setText(null);
            }
            if (e.getSource() == confirm) {
                String s1 = new String(passwd.getPassword());

                if (name_txt.getText().length() == 0
                        || passwd.getPassword().length == 0
                        || day.getSelectedIndex() == 0
                        || verify_txt.getText().length() == 0) {
                    JOptionPane.showMessageDialog(Login.this,
                            "<html><font color=red size=5>必填内容不能为空");
                    passwd.setText(null);
                    passwd_sec.setText(null);
                } else {
                    String s2 = new String(passwd_sec.getPassword());
                    if (!s1.equals(s2)) {
                        JOptionPane.showMessageDialog(Login.this,
                                "<html><font color=red size=5>两次输入的密码不一样");
                        passwd.setText(null);
                        passwd_sec.setText(null);
                    } else {
                        String s3 = verify_txt.getText();
                        if (!s3.equals(passcode)) {
                            JOptionPane.showMessageDialog(Login.this,
                                    "<html><font color=red size=5>验证码输入错误");
                            passwd.setText(null);
                            passwd_sec.setText(null);
                        } else {
                            UserInfo user = new UserInfo();
                            user.setName(name_txt.getText());
                            user.setPassword(new String(passwd.getPassword()));
                            user.setBirthdate(year.getSelectedItem().toString()
                                    + "年" + month.getSelectedItem().toString()
                                    + "月" + day.getSelectedItem().toString()
                                    + "日");
                            String s = "";
                            for (int i = 0; i < interest.length; i++) {
                                if (interest[i].isSelected()) {
                                    s += interest[i].getText() + "  ";
                                }
                            }
                            if (man.isSelected()) {
                                user.setSex("男");
                            } else {
                                user.setSex("女");
                            }
                            user.setInterest(s);
                            user.setOthers(area.getText());
                            try {
                                FileOutputStream fos = new FileOutputStream(
                                        "UserInfo.txt");
                                ObjectOutputStream oos = new ObjectOutputStream(
                                        fos);
                                oos.writeObject(user);
                                oos.flush();
                                oos.close();
                            } catch (FileNotFoundException e1) {

                                e1.printStackTrace();
                            } catch (IOException e1) {

                                e1.printStackTrace();
                            }
                            try {
                                readInfo("UserInfo.txt");
                            } catch (IOException e1) {

                                e1.printStackTrace();
                            } catch (ClassNotFoundException e1) {

                                e1.printStackTrace();
                            }
                        }
                    }
                }
            }
            if (e.getSource() == fileopen) {
                FileDialog fd = new FileDialog(Login.this, "打开文件",
                        FileDialog.LOAD);
                fd.setVisible(true);
                Image image = Toolkit.getDefaultToolkit().getImage(
                        fd.getDirectory() + fd.getFile()).getScaledInstance(
                        picture.getWidth(), picture.getHeight(),
                        Image.SCALE_SMOOTH);
                picture.setIcon(new ImageIcon(image));
            }
        }
    }

    public void readInfo(String s) throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream(s);
        ObjectInputStream ois = new ObjectInputStream(fis);
        while (true) {
            try {
                UserInfo user = (UserInfo) ois.readObject();
                String string = "";
                string += user.getName() + "\n";
                string += user.getPassword() + "\n";
                string += user.getBirthdate() + "\n";
                string += user.getSex() + "\n";
                string += user.getInterest() + "\n";
                string += user.getOthers() + "\n";
                JOptionPane.showMessageDialog(null, string);
            } catch (EOFException e) {
                return;
            }
        }
    }
} 
